var planCode=getQueryString("planCode")
$(function () {
    $("#jqGrid").Grid({
        url: '../toolscheckrecord/list?toolsRevisionPlanCode='+planCode,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '精度检测编码', name: 'toolsRevisionPlanCode', index: 'TOOLS_REVISION_PLAN_CODE', width: 80},
            {label: '校对编码', name: 'checkCode', index: 'CHECK_CODE', width: 80},
            {label: '工装唯一标示', name: 'toolsOnlyMark', index: 'TOOLS_ONLY_MARK', width: 80},
            {
                label: '最近一次检测日期',
                name: 'lastRevisionTime',
                index: 'LAST_REVISION_TIME',
                width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '送检日期',
                name: 'inspectionTime',
                index: 'INSPECTION_TIME',
                width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '计划返回日期',
                name: 'planReturnTime',
                index: 'PLAN_RETURN_TIME',
                width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '实际返回日期',
                name: 'actualReturnTime',
                index: 'ACTUAL_RETURN_TIME',
                width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '操作人', name: 'createName', index: 'CREATE_USER', width: 80},
            {
                label: '操作时间', name: 'createTime', index: 'CREATE_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '抽查详情', name: 'toolsRevisionPlanCode', index: 'TOOLS_REVISION_PLAN_CODE', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.sampleOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            {label: '抽查状态', name: 'sampleStatus',index:'SAMPLE_STATUS', hidden: true},
            {label: '抽查时间', name: 'sampleTime',index:'SAMPLE_TIME', hidden: true},
            {label: '抽查人', name: 'sampleUser',index: 'SAMPLE_USER', hidden: true}

        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        toolsCheckRecord: {
            updateTime: '',
            updateUser: '',
            createUser: '',
            createTime: '',
            delFlag: '',
            actualReturnTime: '',
            planReturnTime: '',
            inspectionTime: '',
            lastRevisionTime: '',
            toolsOnlyMark: '',
            toolsRevisionPlanCode: '',
            checkCode: '',
            id: '',
            sampleUser: '',
            sampleStatus: 0,
            remark: '',
            type: 0,
        },
        ruleValidate: {
            toolsRevisionPlanCode: [
                {required: true, message: '工装校对计划编码不能为空', trigger: 'blur'}
            ]
        },
        checkData:{
            sampleStatus:'',
            remark:''
        },
        q: {
            toolsCode: ''
        },
        checkList: enumsToList('SAMPLE_STATUS'),//抽查状态
    },
    methods: {
        //抽查详情
        sampleOption: function (res) {
            let data = JSON.parse(unescape(res))
            if (!data.sampleUser) {
                data.sampleUser = "-"
            }
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>抽查人：</td><td>" + data.sampleUser + "</td></tr>" +
                "<tr><td>抽查状态：</td><td>" + sysEnums.SAMPLE_STATUS[data.sampleStatus] + "</td></tr>" +
                "<tr><td>抽查时间：</td><td>" + transDate(data.sampleTime, "yyyy-MM-dd") + "</td></tr>" +
                "<tr><td>抽查备注：</td><td>" + (data.remark?data.remark:'暂无') + "</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "抽查详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsCheckRecord = {
                updateTime: '',
                updateUser: '',
                createUser: '',
                createTime: '',
                delFlag: '1',
                actualReturnTime: '',
                planReturnTime: '',
                inspectionTime: '',
                lastRevisionTime: '',
                toolsOnlyMark: '',
                toolsRevisionPlanCode: '',
                checkCode: '',
                id: '',
                sampleUser: '',
                sampleStatus: 0,
                remark: '',
                type: 0,
            };
            vm.toolsCheckRecord.toolsRevisionPlanCode=planCode
            var date = new Date();
            vm.toolsCheckRecord.checkCode = 'check-' + date.getFullYear()+ (date.getMonth() + 1) +(date.getDate())+ date.getHours()+date.getMinutes()+date.getSeconds()

        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.toolsCheckRecord.id == (null || '') ? "../toolscheckrecord/save" : "../toolscheckrecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsCheckRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolscheckrecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolscheckrecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsCheckRecord = r.toolsCheckRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'toolsCode': vm.q.toolsCode},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                toolsCode: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        //抽查
        sample: function () {
            let ids = getSelectedRow("#jqGrid");
            if (ids == null) {
                return;
            }
            var data=getSelectedRowData("#jqGrid");
            if (data.sampleStatus==='1') {
                iview.Message.error("当前记录已抽查");
            }else{
                //设置默认1审核通过
                vm.checkData.sampleStatus=1;
                vm.checkData.remark='';
                openWindow({
                    title:"抽查",
                    area: ['400px', '300px'],
                    content: jQuery("#check"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        let obj = {'sampleStatus':vm.checkData.sampleStatus,'remark':vm.checkData.remark,'id':data.id};
                        Ajax.request({
                            url:'../toolscheckrecord/sample',
                            params: obj,
                            type: "POST",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            }
                        })
                        layer.close(index);
                    }
                });
            }
        }
    }
});